using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.IFilter
{
    public partial class FileFilterEditor : Form, IComponentEditor 
    {
        private object _OwnerActivityNode;
        private FileContentSearchComponent _FileFilter;

        public FileFilterEditor()
        {
            InitializeComponent();
        }

        private void btn_Browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg=new OpenFileDialog();
            dlg.Multiselect = true;
            if(dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                this.txt_FilePaths.Text = StringUtil.ToString(dlg.FileNames,",");
            }
        }

        private void FileFilterEditor_Load(object sender, EventArgs e)
        {
            this.rd_ExactMatch.Checked = true;
            this.ck_IncludeSubFolders.Checked = false;
        }

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _OwnerActivityNode; }
            set { _OwnerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(FileContentSearchComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars,
            WorkflowComponentBase[] fromComponents, 
            object activityNode, WorkflowComponentBase wfComponent)
        {
            this._OwnerActivityNode = activityNode;
            this._FileFilter = (FileContentSearchComponent) wfComponent;

            this.txt_FilePaths.Text = string.Empty;
            if(this._FileFilter.SourceFilePaths !=null && this._FileFilter.SourceFilePaths.Count>0)
            {
                this.txt_FilePaths.Text = StringUtil.ToString(this._FileFilter.SourceFilePaths.ToArray(), ",");
            }
            this.txt_Pattern.Text = this._FileFilter.FileNamePattern;
            this.ck_IncludeSubFolders.Checked = false;
            if(this._FileFilter.SearchOption !=SearchOption.TopDirectoryOnly)
            {
                this.ck_IncludeSubFolders.Checked = true;
            }
            if(this._FileFilter.StringMatchOption==StringMatchType.RegexMatch)
            {
                this.rd_RegexMatch.Checked = true;
            }
            else
            {
                this.rd_ExactMatch.Checked = true;
            }
        }

        public TestResult Test()
        {
            return new TestResult(true, string.Empty);
        }

        public void SaveSettings()
        {
            this._FileFilter.SourceFilePaths = null;
            if(this.txt_FilePaths.Text.Trim().Length >0)
            {
                this._FileFilter.SourceFilePaths=new List<string>();
                string[] filePaths = StringUtil.Split(this.txt_FilePaths.Text, ",");
                this._FileFilter.SourceFilePaths.AddRange(filePaths);
            }
            this._FileFilter.FileNamePattern = this.txt_Pattern.Text;
            this._FileFilter.SearchOption = this.ck_IncludeSubFolders.Checked
                                                ? SearchOption.AllDirectories
                                                : SearchOption.TopDirectoryOnly;
            if(this.rd_RegexMatch.Checked)
            {
                this._FileFilter.StringMatchOption = StringMatchType.RegexMatch;
            }
            else
            {
                this._FileFilter.StringMatchOption = StringMatchType.ExactMatch;
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent !=null)
            {
                this.UpdateComponentSettingEvent(this._OwnerActivityNode, this._FileFilter);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Test_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            FileContentSearchInstruction instruction =
                new FileContentSearchInstruction(this._FileFilter.ComponentID, Guid.Empty,
                                                 this._FileFilter.SourceFilePaths, this._FileFilter.FileNamePattern,
                                                 this._FileFilter.SearchOption, this._FileFilter.StringMatchOption);
            FileFilterWorker worker=new FileFilterWorker();
            worker.SetInstruction(Guid.Empty, instruction, string.Empty, this._FileFilter.ComponentName);
            worker.WorkerErredEvent += new Common.Utilities.Dispatcher.WorkerHasError(worker_WorkerErredEvent);
            worker.WorkerFinishedEvent += new Common.Utilities.Dispatcher.WorkerFinished(worker_WorkerFinishedEvent);
            worker.DoWork();
        }

        void worker_WorkerFinishedEvent(Guid ticketID, Type workerType, Common.Utilities.Dispatcher.InstructionBase instructionBase, Common.Utilities.Dispatcher.ResultBase resultBase)
        {
            FileContentSearchOutput output = (FileContentSearchOutput) resultBase;
            MessageBox.Show(StringUtil.ToString(output.SearchResults.ToArray()));
        }

        void worker_WorkerErredEvent(Guid ticketID, Type workerType, Common.Utilities.Dispatcher.InstructionBase instructionBase, string error, bool isFatal)
        {
            MessageBox.Show(error);
        }
    }
}